package com.beiming.libra.service.impl;

import com.beiming.libra.common.utils.BeanConvertUtils;
import com.beiming.libra.dao.AreasMapper;
import com.beiming.libra.domain.Areas;
import com.beiming.libra.domain.dto.responsedto.AreasDTO;
import com.beiming.libra.service.AreasService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author Rimon Date : 2018/6/7 11:02
 */
@Service
@Slf4j
public class AreasServiceImpl implements AreasService {

  @Resource
  private AreasMapper areasMapper;

  @Override
  public List<AreasDTO> getChildrenAreasByCode(String code) {
    return areasMapper.getChildrenAreasByCode(code);
  }

  @Override
  public Map<String, Object> getRelationAreasByCode(String code) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("provinces", areasMapper.getAllProvinces());
    //该编码所属省编码
    String provinceCode =
        String.valueOf(Long.parseLong(code) / 100000000) + String.format("%1$0" + 8 + "d", 0);
    //查询该编码所属省下所有市
    List<AreasDTO> cities = areasMapper.getChildrenAreasByCode(provinceCode);
    map.put("cities", cities);
    //查询该编码所属省下所有市的所有区县
    List<AreasDTO> districts = areasMapper.getChildrenAreasByCode(
        String.valueOf(Long.parseLong(code) / 1000000) + String.format("%1$0" + 6 + "d", 0));
//    for (AreasDTO areasDTO : cities) {
//      districts.addAll(areasMapper.getChildrenAreasByCode(areasDTO.getCode()));
//    }
    map.put("districts", districts);
    //查询该编码所属省下所有市的所有区县的所有街道
    List<AreasDTO> streets = areasMapper.getChildrenAreasByCode(
        String.valueOf(Long.parseLong(code) / 10000) + String.format("%1$0" + 4 + "d", 0));
//    for (AreasDTO areasDTO : districts) {
//      streets.addAll(areasMapper.getChildrenAreasByCode(areasDTO.getCode()));
//    }
    map.put("streets", streets);
    List<Areas> currentAreaList = areasMapper.getAddressByCode(code);
    if (currentAreaList.size() > 0) {
      Areas currentArea = currentAreaList.get(0);
      if (4 == currentArea.getLevel()) {
        map.putAll(getDistrict(code));
        map.put("currentStreet", BeanConvertUtils.copyBean(currentArea, AreasDTO.class));
      } else if (3 == currentArea.getLevel()) {
        map.putAll(getCity(code));
        map.put("currentDistrict", BeanConvertUtils.copyBean(currentArea, AreasDTO.class));
        map.put("currentStreet", null);
      } else if (2 == currentArea.getLevel()) {
        map.putAll(getProvince(code));
        map.put("currentCity", BeanConvertUtils.copyBean(currentArea, AreasDTO.class));
        map.put("currentDistrict", null);
        map.put("currentStreet", null);
      } else {
        map.put("currentProvince", BeanConvertUtils.copyBean(currentArea, AreasDTO.class));
        map.put("currentCity", null);
        map.put("currentDistrict", null);
        map.put("currentStreet", null);
      }
    }
    return map;
  }

  @Override
  public List<AreasDTO> getAllProvinces() {
    return areasMapper.getAllProvinces();
  }

  private Map<String, Object> getProvince(String code) {
    Map<String, Object> map = new HashMap<String, Object>();
    String provinceCode =
        String.valueOf(Long.parseLong(code) / 100000000) + String.format("%1$0" + 8 + "d", 0);
    List<Areas> currentAreaList = areasMapper.getAddressByCode(provinceCode);
    Areas currentArea = currentAreaList.get(0);
    map.put("currentProvince", BeanConvertUtils.copyBean(currentArea, AreasDTO.class));
    return map;
  }

  private Map<String, Object> getCity(String code) {
    Map<String, Object> map = new HashMap<String, Object>();
    String cityCode =
        String.valueOf(Long.parseLong(code) / 1000000) + String.format("%1$0" + 6 + "d", 0);
    List<Areas> currentAreaList = areasMapper.getAddressByCode(cityCode);
    Areas currentArea = currentAreaList.get(0);
    map.putAll(getProvince(code));
    map.put("currentCity", BeanConvertUtils.copyBean(currentArea, AreasDTO.class));
    return map;
  }

  private Map<String, Object> getDistrict(String code) {
    Map<String, Object> map = new HashMap<String, Object>();
    String districtCode =
        String.valueOf(Long.parseLong(code) / 10000) + String.format("%1$0" + 4 + "d", 0);
    List<Areas> currentAreaList = areasMapper.getAddressByCode(districtCode);
    Areas currentArea = currentAreaList.get(0);
    map.putAll(getProvince(code));
    map.putAll(getCity(code));
    map.put("currentDistrict", BeanConvertUtils.copyBean(currentArea, AreasDTO.class));
    return map;
  }
}
